<?php
/**
  * Mar 13, 2008
  *
  * Coded by: Ropu
  *
  */
include 'login.php';
$errors = array();
if(isset($_GET['id'])) {
  $apps = $_SESSION['hacker']->getApps();
  if(isset($apps[(int)$_GET['id']])) {
    $app = $apps[(int)$_GET['id']];
  } 
}

if(isset($_POST['version'])) {
  
  $appV = new AppVersion();
  if(!empty($_POST['version'])) {
    $appV->setVersion($_POST['version']);
    
    if(!empty($_POST['url'])) {
      $appV->setUrl($_POST['url']);
      $appV->setUploaded(false);
    } else if(isset($_FILES['upload'])){
      try {
        $appV->upload($_FILES['upload']);
        $appV->setUploaded(true);
      } catch (Exception $e) {
        $errors['upload'] = $e->getMessage();
      }
    } else {
      $errors['upload'] = $lang['appedit']['upload_or_url'];
    }
    
    
    $appV->setOpensource(isset($_POST['opensource']));
    $appV->setPublic(isset($_POST['public']));
    
    if(!isset($app)) {
      if(empty($_POST['title'])) {
        $errors['title'] = $lang['appedit']['title_needed'];
      }
      
      if(empty($_POST['description'])) {
        $errors['description'] = $lang['appedit']['description_needed'];
      }
      
      $app = new App();
      $app->setName('App Name (no use)');
      $app->setTitle(@$_POST['title']);
      $app->setDescription(@$_POST['description']);
      $app->setThumb(WS_HOME_DIR . 'images/gadget_default_icon.png');
    }
    
    if(empty($errors)) {
      $app->newVersion($appV);
      $_SESSION['hacker']->addApp($app);
    }
  } else {
    $errors['version'] = $lang['appedit']['version_needed'];
  }
}
$fill = $_POST;

//  $hacker = new Hacker();
include 'templates/temp_appedit.php';